package com.dondown.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.easystudy.sign.conf.SystemConfig;
import com.easystudy.sign.error.ReturnValue;
import com.easystudy.sign.model.DocInfo;
import com.easystudy.sign.model.FileInfo;
import com.easystudy.sign.model.FlowConfig;
import com.easystudy.sign.model.FlowInfo;
import com.easystudy.sign.model.FlowStatus;
import com.easystudy.sign.model.NoticeConfig;
import com.easystudy.sign.model.OrgInfo;
import com.easystudy.sign.model.OrgSignerInfo;
import com.easystudy.sign.model.Organization;
import com.easystudy.sign.model.RedirectConfig;
import com.easystudy.sign.model.SignConfig;
import com.easystudy.sign.model.SignFieldConfig;
import com.easystudy.sign.model.SignFieldInfo;
import com.easystudy.sign.model.SignFile;
import com.easystudy.sign.model.SignUrl;
import com.easystudy.sign.model.SignerInfo;
import com.easystudy.sign.model.TransactorInfo;
import com.easystudy.sign.model.UploadInfo;
import com.easystudy.sign.param.CreateFlowParam;
import com.easystudy.sign.param.FileParam;
import com.easystudy.sign.param.GetSignUrlParam;
import com.easystudy.sign.service.SignService;
import com.easystudy.sign.util.DateUtil;

/**
 * @文件名称: SignController.java
 * @功能描述: e签宝测试控制器
 * @版权信息: www.easystudy.com
 * @技术交流: 961179337(QQ群)
 * @编写作者: lixx2048@163.com
 * @联系方式: 941415509(QQ)
 * @开发日期: 2021年6月11日
 * @历史版本: V1.0 
 * @备注信息:
 */
@RestController
@RequestMapping("/sign")
public class SignController {
	@Autowired
	private SignService signService;
	@Autowired
	private SystemConfig systemConfig;
	
	@GetMapping("/UploadFile")
	public ReturnValue<String> UploadFile(@RequestParam(name="filePath") String filePath) {
		// 获取文件上传地址
		FileParam file = new FileParam();
		file.setFilePath(filePath);
		ReturnValue<UploadInfo> uploadInfo = signService.getUploadUrl(file);
		if (uploadInfo != null && uploadInfo.getError() == 0) {
			// 上传文件至服务
			ReturnValue<String> uploadFile = signService.uploadFile(filePath, uploadInfo.getValue().getFileUploadUrl());
			if (uploadFile != null && uploadFile.getError() != 0) {
				return new ReturnValue<String>(uploadInfo.getError(), uploadInfo.getDescription());
			}
			// 返回文件编码
			return new ReturnValue<String>(uploadInfo.getValue().getFileId());
		}
		// 返回错误码描述
		return new ReturnValue<String>(uploadInfo.getError(), uploadInfo.getDescription());
	}
	
	@GetMapping("/GetFileInfo")
	public ReturnValue<FileInfo> GetFileInfo(@RequestParam(name="fileId") String fileId) {
		return signService.getFileInfo(fileId, false);
	}
	
	@GetMapping("/CreateSignFlow")
	public ReturnValue<FlowInfo> CreateSignFlow(
			@RequestParam(name="fileId") String fileId,
			@RequestParam(name="fileName") String fileName,
			@RequestParam(name="signFlowTitle") String signFlowTitle) {
		
		// 设置文档列表
		List<DocInfo> docs = new ArrayList<DocInfo>();
		DocInfo doc = new DocInfo();
		doc.setFileId(fileId);
		doc.setFileName(fileName);
		docs.add(doc);
		
		// 重定向配置
		RedirectConfig redirectConfig = new RedirectConfig();
		redirectConfig.setRedirectUrl(systemConfig.getRedirectUrl());
		
		// 计算过期时间：不能超过90天
		Date nowTime = new Date(System.currentTimeMillis());
		nowTime = DateUtil.addDay(nowTime, 90);
		
		// 签署流程配置:1715407299000
		FlowConfig signFlowConfig = new FlowConfig();
		signFlowConfig.setSignFlowTitle(signFlowTitle);
		signFlowConfig.setSignFlowExpireTime(nowTime.getTime());
		signFlowConfig.setAutoFinish(true);
		signFlowConfig.setNotifyUrl(systemConfig.getNotifyUrl());
		signFlowConfig.setRedirectConfig(redirectConfig);
		
		// 签署方列表
		List<SignerInfo> signers = new ArrayList<SignerInfo>();
		for (int i = 0; i < 1; i++) {
			// 签署方信息
			SignerInfo signer = new SignerInfo();
			
			// 签署方配置
			SignConfig signConfig = new SignConfig();
			signConfig.setSignOrder(i+1);
			signConfig.setSignTaskType(0);	// 0会签 1或签
			signer.setSignConfig(signConfig);
			
			// 签署方通知: 短信1+邮件2
			NoticeConfig noticeConfig = new NoticeConfig();
			noticeConfig.setNoticeTypes("1,2");
			signer.setNoticeConfig(noticeConfig);
			
			// 组织签署信息：
			OrgSignerInfo orgSignerInfo = new OrgSignerInfo();
			orgSignerInfo.setOrgName("佛山市掌控易智能科技有限公司");
			
			OrgInfo orgInfo = new OrgInfo();
			orgInfo.setOrgIDCardNum("9144040077186320XM");
			orgInfo.setOrgIDCardType("CRED_ORG_USCC");
			orgSignerInfo.setOrgInfo(orgInfo);
			
			TransactorInfo transactorInfo = new TransactorInfo();
			transactorInfo.setPsnAccount("18038856702");
			orgSignerInfo.setTransactorInfo(transactorInfo);
			
			signer.setOrgSignerInfo(orgSignerInfo);
			
			// 签署区域
			List<SignFieldInfo> signFields = new ArrayList<>();
			
			SignFieldInfo signFieldInfo = new SignFieldInfo();
			signFieldInfo.setCustomBizNum(java.util.UUID.randomUUID().toString());
			signFieldInfo.setFileId(fileId);
			SignFieldConfig normalSignFieldConfig = new SignFieldConfig();
			normalSignFieldConfig.setFreeMode(true);
			signFieldInfo.setNormalSignFieldConfig(normalSignFieldConfig);
			signFields.add(signFieldInfo);
			
			signer.setSignFields(signFields);
			
			// 签署方类型：个人0，企业1
			signer.setSignerType(1);
			
			// 加入签署列表
			signers.add(signer);
		}
		
		// 流程签署信息
		CreateFlowParam flow = new CreateFlowParam();
		// 设置签署文档
		flow.setDocs(docs);
		// 设置流程配置
		flow.setSignFlowConfig(signFlowConfig);
		// 签署方列表
		flow.setSigners(signers);
		
		// 发起流程签署
		return signService.createSignFlow(flow);
	}
	
	@GetMapping("/GetSignUrl")
	public ReturnValue<SignUrl> GetSignUrl(@RequestParam(name="flowId") String flowId) {
		// 获取签名参数
		GetSignUrlParam param = new GetSignUrlParam();
		param.setClientType("ALL");
		param.setUrlType(2);
		param.setNeedLogin(false);
		
		// 企业信息
		Organization organization = new Organization();
		organization.setOrgName("佛山市掌控易智能科技有限公司");
		param.setOrganization(organization);
		
		// 企业经办人
		TransactorInfo operator = new TransactorInfo();
		operator.setPsnAccount("18038856702");
		param.setOperator(operator);
		
		// 重定向配置
		RedirectConfig redirectConfig = new RedirectConfig();
		redirectConfig.setRedirectUrl(systemConfig.getRedirectUrl());
		param.setRedirectConfig(redirectConfig);
		
		// 获取签署地址
		return signService.getSignUrl(flowId, param);
	}
	
	@GetMapping("/GetFlowDetail")
	public ReturnValue<FlowStatus> GetFlowDetail(@RequestParam(name="flowId") String flowId) {
		return signService.getFlowDetail(flowId);
	}
	
	@GetMapping("/GetSignedFlowFile")
	public ReturnValue<SignFile> GetSignedFlowFile(@RequestParam(name="flowId") String flowId) {
		return signService.getSignedFlowFile(flowId);
	}
	
	@GetMapping("/removeFlow")
	public ReturnValue<String> RemoveFlow(@RequestParam(name="flowId") String flowId,
			@RequestParam(name="revokeReason") String revokeReason) {
		return signService.removeFlow(flowId, revokeReason);
	}
}
